package nl.xs4all.dylanschell.deckprinter.parser;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import nl.xs4all.dylanschell.deckprinter.model.CardInDeck;
import nl.xs4all.dylanschell.deckprinter.model.DeckDescriptor;
import nl.xs4all.dylanschell.deckprinter.services.DeckParser;

import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;

@Singleton
public class MWDeckParser implements DeckParser
{
    private FileNameExtensionFilter filter;

    @Inject
    public MWDeckParser()
    {
        filter = new FileNameExtensionFilter(
                "Magic Workstation deck files (*.mwdeck)", "mwdeck");
    }

    public DeckDescriptor parseDeck(URL url, String name)
    {
        try
        {
            return parseDeck(url.openStream(), name);
        } catch (IOException e)
        {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return null;
    }

    public DeckDescriptor parseDeck(InputStream is, String name)
    {
        DeckDescriptor result = new DeckDescriptor(name);
        Reader reader;
        reader = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(reader);
        String line;
        try
        {
            while ((line = br.readLine()) != null)
            {
                parseMwDeckCard(line, result);
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                reader.close();
            } catch (IOException ignored)
            {
            }
        }
        return result;
    }

    private void parseMwDeckCard(String line, DeckDescriptor deck)
    {
        boolean sideBoard = false;
        line = line.trim();
        if (line.startsWith("//"))
        {
            return;
        }
        if (line.length() == 0)
        {
            return;
        }
        if (line.startsWith("SB:"))
        {
            sideBoard = true;
            line = line.substring(4);
        }
        int endAmount = line.indexOf(' ');
        String amount = "";
        if (endAmount >= 0)
        {
            amount = line.substring(0, endAmount);
        }
        int endSet = line.indexOf(']');
        String set = null;
        if (endSet >= 0)
        {
            set = line.substring(endAmount + 2, endSet);
        } else
        {
            endSet = endAmount - 1;
        }
        String name = line.substring(endSet + 2);
        int count;
        try
        {
            count = Integer.parseInt(amount);
        } catch (NumberFormatException nfe)
        {
            count = 1;
            name = amount + ' ' + name;
        }
        // clean up land lines
        if (name.endsWith(")"))
        {
            name = name.replaceAll("\\(", "");
            name = name.replaceAll("\\)", "");
            name = name.replaceAll(" ", "");
        }
//        UUID setId = setService.getSetId(set);
//        CardInDeck temp = new CardInDeck(name, count, null);
//        CardInSet cis = setService.getCardInSet(temp);
//        if (cis == null)
//        {
//            throw new RuntimeException("Unknown card: " + name);
//        }
        CardInDeck result = new CardInDeck(name, count, null);
        if (sideBoard)
        {
            deck.getSideboard().add(result);
        } else
        {
            deck.getMain().add(result);
        }
    }

    public boolean accept(File pathname)
    {
        return pathname.isFile()
                && pathname.getName().toLowerCase().endsWith(".mwdeck");
    }

    public FileNameExtensionFilter getFilter()
    {
        return filter;
    }
}
